<template>
	<view class="content">
		<input type="text" placeholder="请输入标题" v-model="messageItem.title" class="input-field">
		<textarea placeholder="请输入内容" v-model="messageItem.content" class="textarea-field"></textarea>
		<button type="primary" @click="submit" class="submit-button">发布</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				messageItem: {
					title: '',
					content: '',
					name: 'mark',
					date: ''
				}
			}
		},
		methods: {
			submit() {
				this.getCurrentTime();
				uni.request({
					url: 'http://localhost:8090/freshmenapp/message/addone',
					method: 'POST',
					data: this.messageItem,
					success: res => {
						console.log(res.data);
						if (res.data.status === true) {
							uni.showToast({
								title: '发表成功',
								icon: "success"
							});
							this.messageItem.title = '';
							this.messageItem.content = '';
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '网络错误',
							icon: 'error'
						});
					},
				})
			},
			getCurrentTime() {
				let now = new Date();
				let year = now.getFullYear();
				let month = String(now.getMonth() + 1).padStart(2, '0');
				let day = String(now.getDate()).padStart(2, '0');
				this.messageItem.date = `${year}-${month}-${day}`;
			},
		},
		onLoad() {
			this.getCurrentTime();
			// Example onLoad action, adjust as needed
			uni.request({
				url: 'http://localhost:8090/freshmenapp/user/list',
				method: 'POST',
				data: {},
				success: res => {
					console.log(res.data);
				},
				fail: err => {
					console.error('Failed to fetch user list', err);
				}
			});
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
    margin-top: 10px;
	}

	.input-field,
	.textarea-field {
		width: 80%;
		padding: 10px;
		margin-bottom: 20px;
		border: 1px solid #ccc;
		border-radius: 4px;
	}

	.submit-button {
		width: 15%;
		padding: 0 10px;
		background-color: #007bff;
		color: white;
	}

	.submit-button:hover {
		background-color: #0056b3;
	}
</style>